import { FC } from "react";
import type { FormProps } from "antd";
import { Button, Form, Input, InputNumber, message, Upload } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import TextArea from "antd/es/input/TextArea";
import { httpPost } from "@utils/HttpUtil";
import { getUploadUrl } from "@utils/WeStudyUtil";

type FieldType = {
  title?: string;
  line?: number;
  contentImg?: any;
  contentText?: string;
  answerImg?: any;
  answerText?: string;
};

const onFinish: FormProps<FieldType>["onFinish"] = (values) => {
    if (values.contentImg !== undefined && values.contentImg !== null) {
      var id = values.contentImg[0].response.data;
      values.contentImg = id;
    }
    if (values.answerImg !== undefined && values.answerImg !== null) {
      var id = values.answerImg[0].response.data;
      values.answerImg = id;
    }
    console.log(values);
    httpPost("/problem/submitProblem", {
      title: values.title,
      type: "solution",
      content: values,
    });
    message.success("新增题目成功");
};

const onFinishFailed: FormProps<FieldType>["onFinishFailed"] = (errorInfo) => {
    console.log("Failed:", errorInfo);
    message.error("新增题目失败" + errorInfo);
};

const normFile = (e:any) =>{
    if(Array.isArray(e)){
        return e;
    }
    return e && e.fileList;
}

const SubmitSolution: FC = () => {

  const uploadUrl = getUploadUrl();

  return (
    <>
      <Form
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 16 }}
        style={{ maxWidth: 600, marginTop: 40 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item<FieldType>
          label="题目"
          name="title"
          rules={[{ required: true, message: "请输入题目!" }]}
        >
          <Input placeholder="" />
        </Form.Item>
        <Form.Item<FieldType>
          label="答案行数"
          name="line"
          rules={[{ required: true, message: "请输入答案行数!" }]}
        >
          <InputNumber min={1} max={10} defaultValue={3} onChange={()=>{}} changeOnWheel />
        </Form.Item>
        <Form.Item<FieldType>
          label="内容（图片）"
          name="contentImg"
          rules={[{ required: false, message: "请上传题目内容截图!" }]}
          // 下面两条必须存在
          valuePropName="fileList"
          // 如果没有这一句会报错 Uncaught TypeError: (fileList || []).forEach is not a function
          getValueFromEvent={normFile}
        >
          <Upload
            action={uploadUrl}
            listType="picture"
            maxCount={1}
          >
            <Button type="primary" icon={<UploadOutlined />}>
              上传内容截图
            </Button>
          </Upload>
        </Form.Item>

        <Form.Item<FieldType>
          label="内容（文字）"
          name="answerText"
          rules={[{ required: false, message: "请输入题目内容!" }]}
        >
          <TextArea
            rows={6}
            placeholder={"内容（图片）和内容（文字）任选其一提交即可"}
            maxLength={1000}
          />
        </Form.Item>

        <Form.Item<FieldType>
          label="答案（图片）"
          name="answerImg"
          rules={[{ required: false, message: "请上传题目答案截图!" }]}
          // 下面两条必须存在
          valuePropName="fileList"
          // 如果没有这一句会报错 Uncaught TypeError: (fileList || []).forEach is not a function
          getValueFromEvent={normFile}
        >
          <Upload
            action={uploadUrl}
            listType="picture"
            maxCount={1}
          >
            <Button type="primary" icon={<UploadOutlined />}>
              上传答案截图
            </Button>
          </Upload>
        </Form.Item>

        <Form.Item<FieldType>
          label="答案（文字）"
          name="answerText"
          rules={[{ required: false, message: "请输入题目答案!" }]}
        >
          <TextArea
            rows={6}
            placeholder={"答案（图片）和答案（文字）任选其一提交即可"}
            maxLength={1000}
          />
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
          <Button type="primary" htmlType="submit">
            新增题目
          </Button>
        </Form.Item>
      </Form>
    </>
  );
};

export default SubmitSolution;
